﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections;

namespace FooFun.DataElements.Dtos
{
    public class ObjectDataElement : DataElement
    {
        object obj;

        public ObjectDataElement(Object obj)
            : base(obj)
        {

        }

        #region IDataElement 成员

        public override object Value
        {
            get
            {
                return obj;
            }
            set
            {
                obj = value;
            }
        }

        // todo: 反射索引器
        public override IDataElement this[string attrName]
        {
            get
            {
                Type type = obj.GetType();

                PropertyInfo propertyInfo
                    = type.GetProperty
                    ("Item", new Type[] { typeof(string) });

                Object property;

                if (propertyInfo != null)
                {
                    property = propertyInfo.GetValue(
                       this,
                       new object[] { attrName });

                    ObjectDataElement objDE
                        = new ObjectDataElement(property);

                    // todo:...
                    //objDE.Parent = this;

                    return objDE;
                }
                else
                {
                    propertyInfo = type.GetProperty(attrName);
                    if (propertyInfo != null)
                    {
                        property = propertyInfo.GetValue
                            (this.obj, null);

                        return new ObjectDataElement(property);
                    }
                }

                return base[attrName];
            }

            // todo: 错！！！！！！！
            set
            {
                Type type = obj.GetType();

                PropertyInfo propertyInfo
                     = type.GetProperty
                     ("Item", new Type[] { typeof(string) });

                if (propertyInfo != null)
                {
                    propertyInfo.SetValue(
                       this,
                       value,
                       new object[] { attrName });
                }
                else
                {
                    propertyInfo = type.GetProperty(attrName);
                    if (value.Value != null
                        && !(value.Value is DBNull))
                    {
                        propertyInfo.SetValue
                            (this.Value, value.Value, null);
                    }
                }
            }
        }

        public override bool IsEmpty
        {
            get { return false; }
        }

        #endregion

        #region IEnumerable 成员

        public override IEnumerator GetEnumerator()
        {
            if (this.obj is IEnumerable)
            {
                IEnumerable io = obj as IEnumerable;
                foreach (object ele in io)
                {
                    yield return ele;
                }
            }
            else
            {
                yield return this;
            }
        }

        #endregion
    }
}
